define(['./../initial', './Project', './LocationPoints'], function() {
    var app = angular.module('App.home');
    app.factory('modelTransform', ['Project', 'LocationPoints',
        function(Project, LocationPoints) {
            var parseObject = function(jsonResult, key) {
                if (!key) key = 'project';
                var constructor = constructorCollections[key];
                // if (!constructor) console.log('this constructor %s is not includ in collections', key);
                if (!constructor) return jsonResult;
                else {
                    var model = new constructor();
                    angular.extend(model, jsonResult);
                    for (var itemKey in model) {
                        var item = model[itemKey];
                        if (angular.isArray(item)) {
                            model[itemKey] = parseArray(model[itemKey], itemKey);
                        } else if (angular.isObject(item)) {
                            model[itemKey] = parseObject(model[itemKey], itemKey);
                        }
                    }
                    return model;
                }

            };
            var parseArray = function(jsonResult, key) {
                var models = [];
                if (jsonResult.length === 0) return models;
                if (angular.isObject(jsonResult[0])) {
                    angular.forEach(jsonResult, function(object) {
                        var item = parseObject(object, key);
                        models.push(item);
                    });
                } else {
                    models = jsonResult;
                }
                return models;
            };
            var parseData = function(jsonResult, key) {
                if (angular.isArray(jsonResult)) {
                    return parseArray(jsonResult, key);
                } else {
                    return parseObject(jsonResult, key);
                }
            };
            //一些简单类的定义
            var MonitorPoints = function() {
                this.monitorType = '';
                this.locationPoints = [];
            };
            var Points = function() {
                this.name = "";
                this.ringNumber = "";
                this.alert = null;
            };
            var Center = function() {
                this.lat = null;
                this.lng = null;
            };
            var RecentChanges = function() {
                this.lastTime = '';
                this.lastValues = [];
                this.curTime = '';
                this.curValues = [];
                this.max = null;
                this.min = null;
                this.allTimes = []; //get it when first time query.then give it to locationPoints.allTimes;
                this.title = "";
            };

            var constructorCollections = {
                project: Project,
                monitorPoints: MonitorPoints,
                locationPoints: LocationPoints,
                points: Points,
                recentChanges: RecentChanges,
                center: Center
            };

            return parseData;
        }
    ]);
});
